package com.hlhj.funlike_business.activity.fragment;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.mymvp.mvp.BaseFgm;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.AllOrderAdp;
import com.hlhj.funlike_business.bean.OrderListBean;
import com.hlhj.funlike_business.presenter.WaitTakeFragmentPresenter;
import com.hlhj.funlike_business.views.WaitTakeFragmentView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static com.hlhj.funlike_business.activity.fragment.OrderFragment.takeoder;

public class WaitTakeFragment extends BaseFgm<WaitTakeFragmentView,WaitTakeFragmentPresenter> implements WaitTakeFragmentView {
    private SpringView spring;
    private RecyclerView recyclerView;
    private int page;
    private AllOrderAdp allOrderAdp;
    private List<OrderListBean.DataBeanX.DataBean> dataBeanList;
    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_waittake;
    }

    @NotNull
    @Override
    public WaitTakeFragmentView bindView() {
        return this;
    }

    @NotNull
    @Override
    public WaitTakeFragmentPresenter createPresenter() {
        return new WaitTakeFragmentPresenter();
    }

    @Override
    public void initView() {
        spring=rootView.findViewById(R.id.spring);
        recyclerView=rootView.findViewById(R.id.recycle_list);
    }

    @Override
    public void logicStart() {
        spring.setHeader(new DefaultHeader(getContext()));
        spring.setHeader(new DefaultFooter(getContext()));
        dataBeanList = new ArrayList<>();
        allOrderAdp=new AllOrderAdp(dataBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(allOrderAdp);

        getPresenter().LoadOrderList(getContext(),21,page,10);
        allOrderAdp.setListener(new AllOrderAdp.getResult() {
            @Override
            public void showResult(int order_id, int status) {
                getPresenter().LoadResult(getContext(),order_id,status);
            }
        });

        allOrderAdp.setClick(new AllOrderAdp.getChageBack() {
            @Override
            public void showChargeBack(int order_id) {
                getPresenter().LoadChargeBack(getContext(),order_id);
            }
        });
    }

    @Override
    public void setListener() {
        spring.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                getPresenter().LoadOrderList(getContext(),21,page,10);
                spring.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                page++;
                getPresenter().LoadOrderList(getContext(),21,page,10);
                spring.onFinishFreshAndLoad();
            }
        });
    }

    @Override
    public void showOrderList(OrderListBean.DataBeanX dataBeans) {
        if (page==1){
            dataBeanList.clear();
            dataBeanList.addAll(dataBeans.getData());
        }else {
            dataBeanList.addAll(dataBeans.getData());
        }
        takeoder=dataBeans.getTotal();
        EventBus.getDefault().post("刷新待配送");
        allOrderAdp.notifyDataSetChanged();
    }

    @Override
    public void showResult(String msg) {
        Toast.makeText(getContext(),msg.toString(),Toast.LENGTH_SHORT).show();
        getPresenter().LoadOrderList(getContext(),21,page,10);
    }
}
